<template>
	<view class="detail-page">
		<view class="filter">
			<view class="tabs">
				<uv-tabs :list="tabList" lineColor="#FFC434" :scrollable="false" :inactiveStyle="inactiveStyle"
					:activeStyle="inactiveStyle" lineWidth="78rpx" lineHeight="8rpx" @click="tabChange"></uv-tabs>
			</view>
			<view class="dates flex align-center">
				<view class="date" @click="handChooseTime('startTime')">
					<text>{{ queryForm.startTime }}</text>
					<uv-icon name="arrow-down-fill" color="#C2C2C2" size="22rpx"></uv-icon>
				</view>
				<view class="and">至</view>
				<view class="date" @click="handChooseTime('endTime')">
					<text>{{ queryForm.endTime }}</text>
					<uv-icon name="arrow-down-fill" color="#C2C2C2" size="22rpx"></uv-icon>
				</view>
			</view>
		</view>
		<common-list :isDataEmpty="tableData.length === 0" :status="status">
			<template #list>
				<view>
					<view class="list">
						<view class="card" v-for="(item, index) in tableData" :key="index">
							<view class="cell">
								<view class="info">
									<template v-if="curren === 0">
										<view class="orderno">订单编号: {{ item.orderNo }}</view>
										<view class="name">{{ item.content }}</view>
									</template>
									<template v-else>
										<view class="name">
											{{ item.type === 'BROKERAGE' ? '佣金' : '收益' }}提现到{{ item.newBankCard }}[{{ item.openingBank }}]
										</view>
									</template>
									<view class="date">{{ item.createTime }}</view>
								</view>
								<view class="right-info">
									<view class="status">
										<text style="color: #FF9113;" v-if="item.status === 'AUDIT'">审核中</text>
										<text style="color: #19be6b;"
											v-else-if="item.status === 'AUDIT_PASS'">打款成功</text>
										<text style="color: #fa3534;"
											v-else-if="item.status === 'AUDIT_REJECT'">打款失败</text>
									</view>
									<view class="price" v-if="curren === 0">+{{ item.money }}</view>
									<view class="price" v-else>-{{ item.balance }}</view>
								</view>
							</view>
							<view v-if="item.status === 'AUDIT_REJECT'" class="reson">原因: {{ item.reason }}</view>
						</view>
					</view>
				</view>
			</template>
		</common-list>
		<uv-datetime-picker ref="datetimePicker" v-model="showTime" mode="date" @confirm="confirm">
		</uv-datetime-picker>
	</view>
</template>

<script setup>
	import {
		ref,
		reactive
	} from 'vue'
	import {
		doWidthList,
		doShiopLogList
	} from '@/apis/user.js'
	import {
		userList
	} from '@/hooks/userList.js'
	import {
		onLoad,
		onReachBottom,
		onPullDownRefresh,
	} from "@dcloudio/uni-app";

	import CommonList from '@/components/commonList/list.vue'

	const showTime = ref('')
	const datetimePicker = ref(null)
	let oldType = ''
	const handChooseTime = (e) => {
		oldType = e
		showTime.value = new Date(queryForm.value[e])
		datetimePicker.value.open()
	}
	const confirm = (e) => {
		if (oldType === 'endTime') {
			const starTime = new Date(queryForm.value.startTime.replace(/\-/g, "/")).getTime()
			const endTime = new Date(uni.$uv.timeFormat(e.value, 'yyyy-mm-dd').replace(/\-/g, "/")).getTime()
			if (starTime > endTime) {
				uni.$uv.toast('结束时间不能早于开始时间')
				return
			}
			queryForm.value[oldType] = uni.$uv.timeFormat(e.value, 'yyyy-mm-dd')
		} else {
			const starTime = new Date(uni.$uv.timeFormat(e.value, 'yyyy-mm-dd').replace(/\-/g, "/")).getTime()
			const endTime = new Date(queryForm.value.endTime.replace(/\-/g, "/")).getTime()
			if (starTime > endTime) {
				uni.$uv.toast('开始时间不能晚于结束时间')
				return
			}
			queryForm.value[oldType] = uni.$uv.timeFormat(e.value, 'yyyy-mm-dd')
		}
		onRefresh()
	}
	const tabList = reactive([{
		name: '收益明细',
	}, {
		name: '提现明细'
	}])
	let curren = ref(0)
	const tabChange = (e) => {
		curren.value = e.index
		onRefresh()
	}
	const inactiveStyle = reactive({
		color: '#333333',
		fontSize: '32rpx',
		fontWeight: '550'
	})

	const maskCardNumber = (cardNumber) => {
		const maskedNumber = cardNumber.replace(/\d(?=\d{4})/g, '*');
		return maskedNumber;
	}

	const getApi = async (params) => {
		const apis = {
			doShiopLogList,
			doWidthList
		}
		const res = await apis[curren.value === 0 ? 'doShiopLogList' : 'doWidthList'](params)
		if (curren.value === 1) {
			res.records.map(el => {
				el.newBankCard = maskCardNumber(el.bankCard)
				return el
			})
		}
		return res
	}
	const {
		tableData,
		status,
		fetchData,
		onBottom,
		onRefresh,
		queryForm
	} = userList({
		apis: {
			getApi
		},
		initForm: {
			startTime: '',
			endTime: '',
			type: 'EARNINGS'
		}
	})

	onPullDownRefresh(() => {
		onRefresh()
	})
	onReachBottom(() => {
		onBottom()
	})

	onLoad(() => {
		const today = new Date();
		queryForm.value.endTime = uni.$uv.timeFormat(today.getTime(), 'yyyy-mm-dd')
		const sevenDaysAgo = new Date(today.getFullYear(), today.getMonth(), today.getDate() - 7);
		queryForm.value.startTime = uni.$uv.timeFormat(sevenDaysAgo.getTime(), 'yyyy-mm-dd')
		onRefresh()
	})
</script>

<style lang="scss" scoped>
	.detail-page {
		.filter {
			position: sticky;
			z-index: 3;
			top: 0;
			padding-bottom: 10rpx;
			border-bottom: 1rpx solid #EEEEEE;
			background-color: #fff;

			.dates {
				padding: 24rpx;

				.date {
					font-size: 28rpx;
					font-family: PingFang SC-Regular, PingFang SC;
					font-weight: 400;
					color: #333333;
					display: flex;
					align-items: center;

					text {
						padding-right: 3rpx;
					}
				}

				.and {
					margin: 0 16rpx;
					font-size: 30rpx;
					font-family: PingFang SC-Bold, PingFang SC;
					font-weight: bold;
					color: #333333;
				}
			}
		}

		.list {
			padding: 24rpx;

			.card {
				padding: 26rpx 0;
				border-bottom: 1rpx solid #EEEEEE;

				.cell {
					display: flex;
					align-items: center;

					.info {
						flex: 1;
						margin-right: 20rpx;
						.orderno {
							padding-bottom: 10rpx;
							font-size: 30rpx;
						}

						.name {
							font-size: 30rpx;
							font-family: PingFang SC-Regular, PingFang SC;
							font-weight: 400;
							color: #333333;
						}

						.date {
							margin-top: 8rpx;
							font-size: 24rpx;
							font-family: PingFang SC-Regular, PingFang SC;
							font-weight: 400;
							color: #999999;
						}
					}

					.right-info {
						.status {
							font-size: 28rpx;
							font-family: PingFang SC-Regular, PingFang SC;
							font-weight: 400;
							color: #333333;
							padding-bottom: 8rpx;
						}

						.price {
							font-size: 32rpx;
							font-family: PingFang SC-Bold, PingFang SC;
							font-weight: bold;
							color: #333333;
						}
					}
				}

				.reson {
					margin-top: 15rpx;
					font-size: 24rpx;
					color: orangered;
				}
			}
		}

	}
</style>